<?php

class ListController extends Controller
{
	public $alquiler;

	
	public $tipoProp;
	public $zonaProp;
	public $oper;
	public $prices = 'ASC';
	
	public function actionIndex()
	{
		
		//$criteria=new CDbCriteria(array(
		//	'condition'=>'status='.Post::STATUS_PUBLISHED,
		//	'order'=>'update_time DESC',
		//	'with'=>'commentCount',
		//));
		//if(isset($_GET['tag']))
		//	$criteria->addSearchCondition('tags',$_GET['tag']);
		
		$cond='';
		$this->alquiler = isset($_POST['chk_alquiler']) and  $_POST['chk_alquiler'] == 'alquiler';

		
		//operacion
		if(isset($_POST['oper']) and $_POST['oper']!=''){
			$this->oper=$_POST['oper'];
			$cond='operacion='.$this->oper;
			//operacion and precio
			
		}
		
		if(isset($_POST['cmb_Precio']) and $_POST['cmb_Precio']!=''){
			$this->prices=$_POST['cmb_Precio'];
		}else{
			$this->prices='ASC';
		}
		
		$criteria=new CDbCriteria(array(
			'order'=>'publicacions.precio '.$this->prices,
			'with'=>array(
       			'publicacions'=>array(
            		'condition'=>$cond,
        		),
    		),
    		'together'=>true,
		));
		
		if(isset($_POST['cmb_Tipo']) and $_POST['cmb_Tipo']!=''){
			$this->tipoProp=$_POST['cmb_Tipo'];
			$criteria->addCondition('tipo_propiedad='.$_POST['cmb_Tipo'], 'AND');
		}
		
		if(isset($_POST['cmb_Zona']) and $_POST['cmb_Zona']!=''){
			$this->zonaProp=$_POST['cmb_Zona'];
			$criteria->addCondition('zona='.$_POST['cmb_Zona'], 'AND');
		}
		
	
		
		$dataProvider=new CActiveDataProvider('Propiedad', array(
			'pagination'=>array(
				'pageSize'=>10,
			),
			'criteria'=>$criteria,
		));

		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
		
		
	}

	public function actionSingleHouse()
	{
		if(isset($_GET['id'])){
			$id = $_GET['id'];
			$_SESSION['id']=$id;
			$unaProp = Propiedad::model()->findByPk($id);
			$tipo=$unaProp->tipoPropiedad;
			$zona=$unaProp->zona0;
			$fotos=$unaProp->fotos;
			$publi=$unaProp->publicacions;
			foreach($publi as $pub){
				$pub->contacto = $pub->contacto0;
			}
			$amenities=$unaProp->amenities;
			$ambientes=$unaProp->ambientes;
			$favs= Favorita::model()->findAllByAttributes(array('usuario'=>Yii::app()->user->id));
			$esFavorita = false;
			foreach($favs as $fav){
				if ($fav->propiedad == $id){
					$esFavorita = true;
				}
			}

			$retorno = array ( "datos"  => array (
										  "id" => $id, 
									 	  "descripcion" => $unaProp->descripcion,
                                          "zona" => $unaProp->zona,
                                          "antiguedad" => $unaProp->antiguedad,
										  "habitaciones" => $unaProp->habitaciones,
										  "banios" => $unaProp->banos,
								   		  "descripcion_breve" => $unaProp->descripcion_breve,
										  "tipoPropiedad" => $tipo->nombre,
										  "zona" => $zona->nombre,
										  "esFavorita" => $esFavorita,
			),
                  		   "fotos" => $fotos,
                  		   "publicaciones"   => $publi,
                           "amenities" => $amenities,
                           "ambientes" => $ambientes
			);
							
		}
		$this->render('singleHouse',array('retorno'=>$retorno));
	}
	
	
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}